package tr.edu.metu.is.mega.client.schedule;

import tr.edu.metu.is.mega.client.core.EditDialog;
import tr.edu.metu.is.mega.client.resource.MEGAResourceBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.widgets.MegaComboBox;
import tr.edu.metu.is.mega.client.widgets.MegaSpinnerField;
import tr.edu.metu.is.mega.client.widgets.MegaTextField;
import tr.edu.metu.is.mega.common.dto.ActivityTemplateDTO;
import tr.edu.metu.is.mega.common.dto.ActivityType;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;

/**
 * Dialog for {@link ActivityTemplateDTO}.
 */
public class ActivityTemplateDialog extends EditDialog<ActivityTemplateDTO> {

    private MEGAResourceBundle resourceBundle = MEGAResources.RESOURCEBUNDLE;

    private FormBinding binding;

    private BeanModelFactory beanModelFactory;

    /**
     * Default constructor.
     */
    public ActivityTemplateDialog() {
        super(ActivityTemplateDTO.class.getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initForm(FormPanel parent, Object... params) {
        setHeight(220);
        setHeading("Activity Template");
        resourceBundle = MEGAResources.RESOURCEBUNDLE;
        beanModelFactory = BeanModelLookup.get().getFactory(ActivityTemplateDTO.class);

        parent.add(createTypeComboBox(ActivityTemplateDTO.FIELD_SUBMISSIONTYPE), new FormData("96%"));

        MegaTextField descriptionTextField = new MegaTextField(ActivityTemplateDTO.FIELD_DESCRIPTION);
        descriptionTextField.setFieldLabel(resourceBundle.activityTemplateDescription());
        parent.add(descriptionTextField, new FormData("96%"));

        MegaSpinnerField dueDateField = new MegaSpinnerField(ActivityTemplateDTO.FIELD_DUEDATE);
        dueDateField.setMaxValue(150);
        dueDateField.setFieldLabel(resourceBundle.activityTemplateDueDate());
        parent.add(dueDateField, new FormData("96%"));

        MegaSpinnerField dueTimeField = new MegaSpinnerField(ActivityTemplateDTO.FIELD_DUETIME);
        dueTimeField.setMaxValue(23);
        dueTimeField.setFieldLabel(resourceBundle.activityTemplateDueTime());
        parent.add(dueTimeField, new FormData("96%"));

        MegaSpinnerField alertTimeField = new MegaSpinnerField(ActivityTemplateDTO.FIELD_ALERT_TIME);
        alertTimeField.setFieldLabel(resourceBundle.activityTemplateAlertTime());
        parent.add(alertTimeField, new FormData("96%"));

        binding = new FormBinding(parent, true);

        ActivityTemplateDTO dto = new ActivityTemplateDTO();

        binding.bind(beanModelFactory.createModel(dto));

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected ActivityTemplateDTO guiToDTO() {
        BeanModel model = (BeanModel) binding.getModel();
        return model.getBean();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dtoToGUI(ActivityTemplateDTO dto) {
        binding.bind(beanModelFactory.createModel(dto));
    }

    /***
     * Returns activity type combobox.
     * 
     * @param boundAttribute field to bind
     * @return activity type comobox
     */
    public static MegaComboBox<BeanModel> createTypeComboBox(String boundAttribute) {

        BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(ActivityType.class);
        MegaComboBox<BeanModel> comboBoxRole = new MegaComboBox<BeanModel>(boundAttribute);
        ListStore<BeanModel> store = new ListStore<BeanModel>();
        for (ActivityType type : ActivityType.values()) {
            store.add(beanModelFactory.createModel(type));
        }
        comboBoxRole.setStore(store);
        comboBoxRole.setFieldLabel("Type");
        comboBoxRole.setDisplayField("description");
        comboBoxRole.setEditable(false);
        comboBoxRole.setForceSelection(true);
        comboBoxRole.setTriggerAction(TriggerAction.ALL);

        return comboBoxRole;
    }

}
